﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using YJKRevitCode.ElmentSearchUtility.LinkDocumentSuite;
using YJKRGeometry;
using SectionSignAndFilled.StairsRoomSection.Kit;

namespace SectionSignAndFilled.StairsRoomSection.DataDefine
{
	public class SectionViewData
	{
		public SectionViewData(View SelView)
		{
			this.m_SectionView = SelView;
		}

		public SectionViewData clone()
		{
			return new SectionViewData(null)
			{
				m_SecLine = this.m_SecLine,
				m_SectionView = this.m_SectionView,
				m_viewDir = this.m_viewDir,
				m_dTopElev = this.DTopElev,
				m_dBottomElev = this.DBottomElev,
				m_minPt = this.MinPt,
				m_maxPt = this.MaxPt,
				m_SecHeith = this.m_SecHeith
			};
		}

		public XYZ MaxPt
		{
			get
			{
				return this.m_maxPt;
			}
			set
			{
				this.m_maxPt = value;
			}
		}

		public XYZ MinPt
		{
			get
			{
				return this.m_minPt;
			}
			set
			{
				this.m_minPt = value;
			}
		}

		public View SectionView
		{
			get
			{
				return this.m_SectionView;
			}
		}

		public double DBottomElev
		{
			get
			{
				return this.m_dBottomElev;
			}
			set
			{
				this.m_dBottomElev = value;
			}
		}

		public double DTopElev
		{
			get
			{
				return this.m_dTopElev;
			}
			set
			{
				this.m_dTopElev = value;
			}
		}

		public Line SecLine
		{
			get
			{
				return this.m_SecLine;
			}
			set
			{
				this.m_SecLine = value;
			}
		}

		public double SecHeith
		{
			get
			{
				return this.m_SecHeith;
			}
			set
			{
				this.m_SecHeith = value;
			}
		}

		public bool InitViewData()
		{
			this.m_viewDir = this.m_SectionView.ViewDirection;
			BoundingBoxXYZ cropBox = this.m_SectionView.CropBox;
			XYZ max = cropBox.Max;
			XYZ min = cropBox.Min;
			Transform transform = cropBox.Transform;
			BoundingBoxXYZ boundingBoxXYZ = SectionViewData.transformMinAndMaxPt(min, max, transform);
			this.m_maxPt = boundingBoxXYZ.Max;
			this.m_minPt = boundingBoxXYZ.Min;
			this.m_dBottomElev = this.m_minPt.Z;
			this.m_dTopElev = this.m_maxPt.Z;
			this.m_SecHeith = this.m_dTopElev - this.m_dBottomElev;
			XYZ minPt = this.m_minPt;
			XYZ xyz = new XYZ(this.m_maxPt.X, this.m_maxPt.Y, this.m_minPt.Z);
			Line line = Line.CreateUnbound(minPt, this.m_SectionView.RightDirection);
			Line line2 = Line.CreateUnbound(xyz, this.m_SectionView.RightDirection);
			XYZ origin = this.m_SectionView.Origin;
			XYZ xyz2 = new XYZ(origin.X, origin.Y, minPt.Z);
			Line line3 = Line.CreateUnbound(xyz2, this.m_SectionView.ViewDirection.Negate());
			double parameter = line3.Project(minPt).Parameter;
			double parameter2 = line3.Project(xyz).Parameter;
			if (Geometry.LessThan(parameter2, parameter))
			{
				line3 = line2;
				XYZ xyzpoint = line3.Project(minPt).XYZPoint;
				this.m_SecLine = Line.CreateBound(xyzpoint, xyz);
			}
			else
			{
				line3 = line;
				XYZ xyzpoint2 = line3.Project(xyz).XYZPoint;
				this.m_SecLine = Line.CreateBound(minPt, xyzpoint2);
			}
			return true;
		}

		public static View CreateSectionView(BaseLinkDocumentResultBean LinkElem, XYZ minPt, XYZ maxPt)
		{
			if (LinkElem.LinkeInstanceId == -1)
			{
				return LinkElem.UseElement.Document.ActiveView;
			}
			Document linkDocument = CommonFun.getLinkDocument(LinkElem);
			string sectionViewName = "剖面" + LinkElem.LinkeInstanceId.ToString();
			Element element = null;
			bool flag = SectionViewData.IsExistViewSection(linkDocument, sectionViewName, ref element);
			if (flag)
			{
				return element as ViewSection;
			}
			ElementId elementId = ElementId.InvalidElementId;
			IList<Element> list = new FilteredElementCollector(linkDocument).OfClass(typeof(ViewFamilyType)).ToElements();
			foreach (Element element2 in list)
			{
				ViewFamilyType viewFamilyType = element2 as ViewFamilyType;
                if (viewFamilyType != null && viewFamilyType.ViewFamily == (ViewFamily)112)
				{
					elementId = element2.Id;
					break;
				}
			}
			Transform transform = LinkElem.GetTotalTransform();
			transform = transform.Inverse;
			if (ElementId.InvalidElementId != elementId)
			{
				BoundingBoxXYZ boundingBoxXYZ = SectionViewData.transformMinAndMaxPt(minPt, maxPt, transform);
				Transaction transaction = new Transaction(linkDocument, "CreateSection");
				try
				{
					transaction.Start();
					ViewSection viewSection = ViewSection.CreateSection(linkDocument, elementId, boundingBoxXYZ);
					viewSection.GetParameter(BuiltInParameter.VIEWER_CROP_REGION).Set(1);
					viewSection.GetParameter(BuiltInParameter.VIEWER_CROP_REGION_VISIBLE).Set(1);
					viewSection.GetParameter(BuiltInParameter.VIEW_DISCIPLINE).Set(4095);
					viewSection.Scale = 50;
					transaction.Commit();
					return viewSection;
				}
				catch (Exception)
				{
					if (transaction.HasStarted())
					{
						transaction.RollBack();
					}
					return null;
				}
			}
			return null;
		}

		public static BoundingBoxXYZ transformMinAndMaxPt(XYZ minPt, XYZ maxPt, Transform transform)
		{
			BoundingBoxXYZ boundingBoxXYZ = new BoundingBoxXYZ();
			XYZ xyz = transform.OfPoint(minPt);
			XYZ xyz2 = transform.OfPoint(maxPt);
			double minOrMaxValue = SectionViewData.GetMinOrMaxValue(xyz.X, xyz2.X, true);
			double minOrMaxValue2 = SectionViewData.GetMinOrMaxValue(xyz.Y, xyz2.Y, true);
			double minOrMaxValue3 = SectionViewData.GetMinOrMaxValue(xyz.Z, xyz2.Z, true);
			double minOrMaxValue4 = SectionViewData.GetMinOrMaxValue(xyz.X, xyz2.X, false);
			double minOrMaxValue5 = SectionViewData.GetMinOrMaxValue(xyz.Y, xyz2.Y, false);
			double minOrMaxValue6 = SectionViewData.GetMinOrMaxValue(xyz.Z, xyz2.Z, false);
			boundingBoxXYZ.Min = new XYZ(minOrMaxValue, minOrMaxValue2, minOrMaxValue3);
			boundingBoxXYZ.Max = new XYZ(minOrMaxValue4, minOrMaxValue5, minOrMaxValue6);
			return boundingBoxXYZ;
		}

		public static double GetMinOrMaxValue(double value1, double value2, bool isMin)
		{
			if (Geometry.LessThan(value1, value2))
			{
				if (isMin)
				{
					return value1;
				}
				return value2;
			}
			else
			{
				if (isMin)
				{
					return value2;
				}
				return value1;
			}
		}

		public static bool IsExistViewSection(Autodesk.Revit.DB.Document doc, string sectionViewName, ref Element findelem)
		{
			List<Element> list = new FilteredElementCollector(doc).OfClass(typeof(ViewSection)).WhereElementIsNotElementType().ToElements() as List<Element>;
			findelem = list.Find((Element e) => (e as ViewSection).ViewName == sectionViewName);
			return findelem != null && findelem.IsValidObject;
		}

		public static Transform GenerateTransformWorldToSection(XYZ viewOrg, XYZ faceNormal)
		{
			Transform identity = Transform.Identity;
			identity.Origin = viewOrg;
			XYZ xyz = faceNormal.Negate();
			XYZ xyz2 = new XYZ(0.0, 0.0, 1.0);
			XYZ xyz3 = xyz2.CrossProduct(xyz);
			identity.set_Basis(0, xyz3);
			identity.set_Basis(1, xyz2);
			identity.set_Basis(2, xyz);
			return identity;
		}

		public XYZ m_viewDir;

		public Line m_SecLine;

		public double m_SecHeith;

		private XYZ m_maxPt;

		private XYZ m_minPt;

		private View m_SectionView;

		private double m_dBottomElev;

		private double m_dTopElev;
	}
}
